package net.xdclass.component;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.config.WechatPayConfig;
import net.xdclass.model.pay.MoneyAmount;
import net.xdclass.model.pay.NativeOrderRequest;
import net.xdclass.rpc.pay.WxPayFeign;
import net.xdclass.util.JsonData;
import net.xdclass.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @author Administrator
 * @description 微信支付
 * @since 2023-08-24 22:54
 */
@Service
@Slf4j
public class WechatPayStrategy implements PayStrategy {

    @Autowired
    private WxPayFeign wxPayFeign;

    @Autowired
    private WechatPayConfig wechatPayConfig;

    @Override
    public String unifiedOrder(PayInfoVO payInfoVO) {

        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        NativeOrderRequest request = new NativeOrderRequest();
        request.setAppid(wechatPayConfig.getWxPayAppid());
        request.setMchid(wechatPayConfig.getMchId());
        request.setDescription(payInfoVO.getDescription());
        request.setOut_trade_no(payInfoVO.getOutTradeNo());
        Duration timeoutDuration = Duration.ofMillis(payInfoVO.getOrderPayTimeoutMills());
        request.setTime_expire(currentTime.plus(timeoutDuration).format(timeFormatter));
        // 附加信息
        JSONObject attach = new JSONObject();
        attach.put("accountNo", payInfoVO.getAccountNo());
        request.setAttach(JSONObject.toJSONString(attach));

        request.setNotify_url(wechatPayConfig.getCallbackUrl());
        request.setGoods_tag("");
        request.setSupport_fapiao(false);

        MoneyAmount amount = new MoneyAmount();
        amount.setTotal(payInfoVO.getPayFee().intValue());
        amount.setCurrency("CNY");

        request.setAmount(amount);

        log.info("请求参数: {}", request);
        JsonData jsonData = wxPayFeign.nativeOrder(request);
        log.info("支付响应: {}", jsonData);
        if (jsonData.getCode().equals(0)) {
            Map<String, Object> result = (Map<String, Object>) jsonData.getData();
            return result.get("pay_url").toString();
        } else {
            log.error("微信支付响应失败: {}", jsonData);
        }
        return "";
    }

    @Override
    public String refund(PayInfoVO payInfoVO) {
        return PayStrategy.super.refund(payInfoVO);
    }

    /**
     * 查询订单状态
     *
     * @param payInfoVO
     * @return
     */
    @Override
    public String queryPayStatus(PayInfoVO payInfoVO) {
        JsonData jsonData = wxPayFeign.queryOrderStatus(wechatPayConfig.getMchId(), payInfoVO.getOutTradeNo());
        if (jsonData.getCode().equals(0)) {
            log.info("查询订单状态成功: {}, 订单状态为: {}", payInfoVO, jsonData.getData());
            return jsonData.getData().toString();
        }
        log.error("查询订单状态失败: {}", payInfoVO);
        return "";
    }

    /**
     * 关闭订单
     * @param payInfoVO
     * @return
     */
    @Override
    public String closeOrder(PayInfoVO payInfoVO) {
        JsonData jsonData = wxPayFeign.closeOrderStatus(wechatPayConfig.getMchId(), payInfoVO.getOutTradeNo());
        if (jsonData.getCode().equals(0)) {
            log.info("关闭订单成功: {}", payInfoVO);
            return "TIMEOUT";
        }
        log.error("关闭订单失败: {}", payInfoVO);
        return "";
    }
}
